do "E:\yungyu\lottery_baby\do\set_environment.do"

clear
set more off


use "$wdata\project_lottery_fertility_b3a6_first_5k.dta", clear
drop if prize10M >= 5

gen prizegrp = 0
replace prizegrp = 1 if prize10M < 0.01 // < 10k
replace prizegrp = 1 if inrange(prize10M,0.01,0.1) // < 1 Million
replace prizegrp = 3 if prize10M >= 0.1

gen n = 1

collapse (mean)totbaby (count)n, by(norm prizegrp current)

reshape wide n totbaby, i(norm prize) j(current)

sort prize norm
order n1 totbaby1 n0 totbaby0 
format totbaby* %9.4f
format n1 n0 %9.0fc
order norm prizegrp n1 totbaby1 n0 totbaby0

rename prizegrp prize1Mup
recode prize1Mup 1=0 3=1

export excel "$table/Tables.xlsx", sheet("Fig1") sheetreplace first(var)

***

import excel using "$table/Tables.xlsx", clear sheet(Fig1) first

forv i = 0(1)1{
	sum totbaby`i' if norm == -1 & prize == 1
	replace totbaby`i' = totbaby`i' - r(mean) if prize == 1
}


twoway 	(scatteri 0 -1 0 6, recast(line) lp(dash) color(gs8)) ///
		(connect totbaby1 norm if prize == 1, ms(O) color(black)), ///
		legend(order(2 "Current Winners (above 1M)" 0 "{  First Difference")) ///
		xtitle(Years from Lottery Prize) xlabel(-3(1)6) ///
		ytitle(Change in Cumulative Number of Children) ylabel(-0.1(0.1)0.3, angle(0) format(%4.1f)) ///
		scheme(s1color) xline(0, lc(black)) ///
		text(0.32 5.8 "⎧", place(s) size(vhuge) color(gs8)) ///
		text(0.25 5.8 "⎪", size(vhuge) color(gs8)) ///
		text(0.2 5.8  "⎪", size(vhuge) color(gs8)) ///
		text(0.15 5.8 "⎨", size(vhuge) color(gs8)) ///
		text(0.1 5.8  "⎪", size(vhuge) color(gs8)) ///
		text(0.05 5.8 "⎪", size(vhuge) color(gs8)) ///
		text(0.01 5.8 "⎩", place(n) size(vhuge) color(gs8)) ///
		text(0.145 5.25 "0.32", color(black)) 
		
	
graph export "$folder/figure/Fig1a.eps", as(eps) replace
graph export "$folder/figure/Fig1a.png", as(png) replace




twoway 	(scatteri 0 -1 0 6, recast(line) lp(dash) color(gs8)) ///
		(connect totbaby1 norm if prize == 1, ms(O) color(black)) ///
		(connect totbaby0 norm if prize == 1, lp(dash) ms(Sh) color(gs8)), ///
		legend(order(2 "Current Winners (above 1M)" 3 "Future Winners (above 1M)")) ///
		xtitle(Years from Lottery Prize) xlabel(-3(1)6) ///
		ytitle(Change in Cumulative Number of Children) ylabel(-0.1(0.1)0.3, angle(0) format(%4.1f)) ///
		scheme(s1color) xline(0, lc(black)) ///
		text(0.255 5.8 "⎧", place(s) size(vhuge) color(gs8)) ///
		text(0.21 5.8  "⎪", size(vhuge) color(gs8)) ///
		text(0.165 5.8 "⎪", size(vhuge) color(gs8)) ///
		text(0.125 5.8 "⎨", size(vhuge) color(gs8)) ///
		text(0.08 5.8  "⎪", size(vhuge) color(gs8)) ///
		text(0.05 5.8  "⎪", size(vhuge) color(gs8)) ///
		text(0.01 5.8  "⎩", place(n) size(vhuge) color(gs8)) ///
		text(0.12 5.25 "0.25", color(black)) 

graph export "$folder/figure/Fig1b.eps", as(eps) replace
graph export "$folder/figure/Fig1b.png", as(png) replace




*--------------------------------------------*
*  Fig 1a — EPS-safe (no Unicode glyphs)     *
*--------------------------------------------*

* Define coordinates
local xbr     = 6      // x position of the bracket
local caphalf = 0.08      // half-length of horizontal caps
local y0      = 0.005      // bottom of bracket
local y1      = 0.31      // top of bracket
local xlab    = `xbr' - 0.4   // x position of the “0.32” label

cap gen y0 = .
cap gen y1 = .
cap gen xbr = .
replace y0 = `y0'
replace xbr = `xbr'
replace y1 = `y1'

twoway ///
    (scatteri 0 -1   0 6, recast(line) lpattern(dash) lcolor(gs8)) ///
    (connect totbaby1 norm if prize==1, msymbol(O) mcolor(black) lcolor(black)) ///
    (pcspike y0 xbr y1 xbr, lcolor(gs8) lwidth(medthick)) ///
    (scatteri `y0' `=`xbr'-`caphalf''  `y0' `=`xbr'+`caphalf'', ///
        recast(line) lcolor(gs8) lwidth(medthick)) ///
    (scatteri `y1' `=`xbr'-`caphalf''  `y1' `=`xbr'+`caphalf'', ///
        recast(line) lcolor(gs8) lwidth(medthick)) ///
    , legend(order(2 "Current Winners (above 1M)" 0 "{  First Difference")) ///
      xtitle("Years from Lottery Prize") xlabel(-3(1)6) ///
      ytitle("Change in Cumulative Number of Children") ///
      ylabel(-0.1(0.1)0.3, angle(0) format(%4.1f)) ///
      xline(0, lcolor(black)) ///
      scheme(s1color) ///
      text(0.145 `xlab' "0.32", color(black))


graph export "$folder/figure/Fig1a.eps", as(eps) replace


*--------------------------------------------*
*  Fig 1b — EPS-safe (no Unicode glyphs)     *
*--------------------------------------------*

* Bracket & label coordinates (tweak if needed)
local xbr     = 6          // x position of the bracket (at year 6)
local caphalf = 0.08       // half-length of bracket caps
local y0      = 0.005      // bracket bottom (≈ 0 on your scale)
local y1      = 0.245      // bracket top (matches "0.25")
local xlab    = `xbr' - 0.40   // x for the "0.25" label

* pcspike needs variables; create constants once
cap gen y0 = .
cap gen y1 = .
cap gen xbr = .
replace y0 = `y0'
replace y1 = `y1'
replace xbr = `xbr'

twoway ///
    (scatteri 0 -1   0 6, recast(line) lpattern(dash) lcolor(gs8)) ///
    (connect totbaby1 norm if prize==1, msymbol(O)  mcolor(black) lcolor(black)) ///
    (connect totbaby0 norm if prize==1, lpattern(dash) msymbol(Sh) mcolor(gs8) lcolor(gs8)) ///
    (pcspike y0 xbr  y1 xbr, lcolor(gs8) lwidth(medthick)) ///
    (scatteri `y0' `=`xbr'-`caphalf''  `y0' `=`xbr'+`caphalf'', recast(line) lcolor(gs8) lwidth(medthick)) ///
    (scatteri `y1' `=`xbr'-`caphalf''  `y1' `=`xbr'+`caphalf'', recast(line) lcolor(gs8) lwidth(medthick)) ///
    , legend(order(2 "Current Winners (above 1M)" 3 "Future Winners (above 1M)")) ///
      xtitle("Years from Lottery Prize")  xlabel(-3(1)6) ///
      ytitle("Change in Cumulative Number of Children") ///
      ylabel(-0.1(0.1)0.3, angle(0) format(%4.1f)) ///
      xline(0, lcolor(black)) ///
      scheme(s1color) ///
      text(0.12 `xlab' "0.25", color(black)) 

graph export "$folder/figure/Fig1b.eps", as(eps) replace
